 <?php
// 请假控制器
class LeaveAction extends MessageAction {
    /**
     * [index description]请假模版跳转
     * @return [type] [description]
     */
    public function index(){
		$this->display('Index:index');
    }

    /**
     * [leaveList description]请假记录
     * @return [type] [description]
     */
    public function leaveList(){
    	$this->display('Leave:leaveList');
    }

    /**
     * [leaveListData description]获取请假列表
     * @return [type] [description]
     */
    public function leaveListData(){
    	$result = array();
    	$result['total'] = $this->getListCount();
    	$result['rows'] = $this->getList();
    	echo json_encode($result);
    }

    /**
     * 获取列表
     */
    public function getList()
    {
    	import("ORG.Util.Page");
    	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    	$result=array();
       	$process=M('Process');
       	$data=$process->where("process_type='Leave' and process_state!=0 and (process_state!=1 or process_now_id_if_approval=0)")->page($page,$rows)->order('process_time desc')->select();
       	foreach ($data as $value) {
       		$value['process_begin_id']=$this->getOneEmployeName($value['process_begin_id']);
       		$value['process_now_id']=$this->getOneEmployeName($value['process_now_id']);
       		$value['process_state']=$this->judgeState($value['process_state']);
       		$result[]=$value;
       	}
        return $result;
    }
    
    /**
     * 获取列表数量
     */
    public function getListCount()
    {	
    	$process=M('Process');
    	$result=$process->where("process_type='Leave' and process_state!=0 and (process_state!=1 or process_now_id_if_approval=0)")->count();
        return $result;
    }

    /**
     * [addLeave description]添加请假单
     */
    public function addLeave(){
        $employe_id=session('employe_id');
        $this->nowEmployeData=$this->getNowEmploye($employe_id);

        $employe_position_id=$this->getOneField('employe_position_id',$employe_id);
        $position_name=$this->getPositionName($employe_position_id);
        $this->assign('employe_position_id',$employe_position_id);
        $this->assign('position_name',$position_name);

        $employe_department_id=$this->getOneField('employe_department_id',$employe_id);
        $dpt_name=$this->getDepartmentName($employe_department_id);
        $this->assign('employe_department_id',$employe_department_id);
        $this->assign('dpt_name',$dpt_name);

        $this->display('Leave:addLeave');
    }

    /**
     * [getDepartmentJson description]获取部门json数据
     * @return [type] [description]
     */
    public function getDepartmentJson(){
        $department=M('Department');
        $data=$department->select();
        $tree=$this->createTreeStuct($data,0);
        echo $this->createJsonString($tree);
    }

    /**
     * [addLeavePost description]处理请假申请
     */
    public function addLeavePost(){
        $leave=M('Leave');
        $process=M('Process');
        $number=$_POST['leave_process_id'];
        $number=intval(substr($number, 0,10));
        $result=$process->where("process_num='".$number."' and process_state=1")->find();//已经申请
        if($result){
            echo '该申请已提交！';
        }
        else{
            $result_second=$process->where("process_num='".$number."' and process_state=0")->find();//如果已经保存了
            if($result_second){
                $dataProcess['process_state']=1;
                $dataLeave['leave_state']=1;
                $process->where("process_num='".$number."'")->save($dataProcess);
                $field=$process->where("process_num='".$number."'")->field('process_id,process_begin_id,process_now_id,process_title')->find();
                $leave->where("leave_process_id='".$field['process_id']."'")->save($dataLeave);

                $msg_from_name=$this->getOneEmployeName($field['process_begin_id']);
                $msg_content=$msg_from_name.'向您提交了一条请假申请，请注意查收';
                if(I('msg_tip')){
                    //发送消息
                    $msg_title=$field['process_title'];
                    $this->sendMsg($field['process_now_id'],$field['process_begin_id'],$msg_title,$msg_content);
                }

                if(I('msg_phone')){
                    //发送短信
                    $mobile=I('phone_num');
                    $content=$msg_content."【来自丽丰OA系统】";
                    $mobileids=$mobile.strval(time());
                    $this->sendSMS($mobile,$content,$mobileids);
                }
                
                echo '申请成功！';
            }
            else{
                $now_time=time();
                $process_begin_id=session('employe_id');
                $process_now_id=I('check_person_id');
                $process->process_num=$number;
                $process->process_title="请假申请";
                $process->process_begin_id=$process_begin_id;
                $process->process_now_id=$process_now_id;
                $process->process_time=$now_time;
                $process->process_state=1;
                $process->process_type='Leave';
                $process->process_now_id_if_approval=0;
                $process->process_parent_id=0;
                if($process_id=$process->add()){
                    
                    $leave->leave_employe_id=session('employe_id');
                    $leave->leave_position_id=I('leave_position_id');
                    $leave->leave_department_id=I('leave_department_id');
                    $leave->leave_type=I('leave_type');
                    $leave->leave_date_num=I('leave_date_num');
                    $leave->leave_begin_time=I('leave_begin_time');
                    $leave->leave_end_time=I('leave_end_time');
                    $leave->leave_reason=I('leave_reason');
                    $leave->leave_time=$now_time;
                    $leave->leave_state=1;
                    $leave->leave_process_id=$process_id;
                    $leave->add();

                    $msg_from_name=$this->getOneEmployeName($process_begin_id);
                    $msg_content=$msg_from_name.'向您提交了一条请假申请，请注意查收';
                    if(I('msg_tip')){
                        //发送消息
                        $msg_title="请假申请";
                        $this->sendMsg($process_now_id,$process_begin_id,$msg_title,$msg_content);
                    }

                    if(I('msg_phone')){
                        //发送短信
                        $mobile=I('phone_num');
                        $content=$msg_content."【来自丽丰OA系统】";
                        $mobileids=$mobile.strval(time());
                        $this->sendSMS($mobile,$content,$mobileids);
                    }

                    echo '申请成功！';
                }    
            }

        }
    }


    /**
     * [saveLeavePost description]保存请假条
     * @return [type] [description]
     */
    public function saveLeavePost(){
        $leave=M('Leave');
        $process=M('Process');
        $number=$_POST['leave_process_id'];
        $number=intval(substr($number, 0,10));
        $result=$process->where("process_num='".$number."'")->find();
        if($result){
            echo '该申请已保存！';
        }
        else{
            $now_time=time();
            $process->process_num=$number;
            $process->process_title="请假申请";
            $process->process_begin_id=session('employe_id');
            $process->process_now_id=I('check_person_id');
            $process->process_time=$now_time;
            $process->process_state=0;
            $process->process_type='Leave';
            $process->process_now_id_if_approval=0;
            $process->process_parent_id=0;

            if($process_id=$process->add()){
                $leave->leave_employe_id=session('employe_id');
                $leave->leave_position_id=I('leave_position_id');
                $leave->leave_department_id=I('leave_department_id');
                $leave->leave_type=I('leave_type');
                $leave->leave_date_num=I('leave_date_num');
                $leave->leave_begin_time=I('leave_begin_time');
                $leave->leave_end_time=I('leave_end_time');
                $leave->leave_reason=I('leave_reason');
                $leave->leave_time=$now_time;
                $leave->leave_state=0;
                $leave->leave_process_id=$process_id;
                $leave->add();
                echo '保存成功！';
            }
        }
    }


    /**
     * [saveLeavePost description]编辑保存的流程
     * @return [type] [description]
     */
    public function savePost(){
        $leave=M('Leave');
        $process=M('Process');
        $number=I('leave_process_id');
        $process_id=I('process_id');

        $result=$process->where("process_id='".$process_id."' and process_state=1")->find();//已经申请
        if($result){
            echo '该申请已提交！';
        }
        else{

            $now_time=time();
            $data['process_num']=$number;
            $data['process_now_id']=I('check_person_id');
            $data['process_time']=$now_time;
            $process->where("process_id='".$process_id."'")->save($data);

            $dataLeave['leave_type']=I('leave_type');
            $dataLeave['leave_date_num']=I('leave_date_num');
            $dataLeave['leave_begin_time']=I('leave_begin_time');
            $dataLeave['leave_end_time']=I('leave_end_time');
            $dataLeave['leave_reason']=I('leave_reason');
            $dataLeave['leave_time']=$now_time;
            $leave->where("leave_process_id='".$process_id."'")->save($dataLeave);
            echo '保存成功';
        }
    }

}